<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var ladderLength = function (beginWord, endWord, wordList) {
      let wordListSet = new Set(wordList)
      if (!wordListSet.has(endWord)) {
        return 0
      }
      let visited = new Set()
      visited.add(beginWord)
      let q = []
      q.push(beginWord)
      let step = 1

      while (q.length > 0) {
        let sz = q.length
        for (let i = 0; i < sz; i++) {
          let curWord = q.shift()
          for (let j = 0; j < curWord.length; j++) {
            let originChar = curWord[j]
            for (let c = 97; c <= 122; c++) {
              if (String.fromCharCode(c) === originChar) continue
              let newWord = ''
              if (j === curWord.length - 1) {
                newWord = curWord.slice(0, j) + String.fromCharCode(c)
              } else {
                newWord = curWord.slice(0, j) + String.fromCharCode(c) + curWord.slice(j + 1)
              }
              if (wordListSet.has(newWord) && !visited.has(newWord)) {
                if (newWord === endWord) {
                  return step + 1
                }
                visited.add(newWord)
                q.push(newWord)
              }

            }
          }        }
        step++
      }

      return 0
    };
    console.log(ladderLength('hit', 'cog', ["hot","dot","dog","lot","log","cog"]));
  </script>
</body>

</html>